{% extends 'base.html' %}

{% block content %}


    {% csrf_token %}
    <div class="add_article">
        <div class="alert-success text-center">所有文章</div>
        <br>
        <div class="add_article_region bs-example">
            <table class="table table-bordered table-hover table-striped">
                <thead>
                <tr>
                    <th>序号</th>
                    <th>文章标题</th>
                    <th>创建时间</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {% for article in article_obj %}
                    <tr class="active warning">
                        <th scope="row">{{ forloop.counter }}</th>
                        <td>{{ article.title }}</td>
                        <td>{{ article.create_time|date:"Y-m-d" }}</td>
                        <td>
                            <a href="/{{ username }}/editor/{{ article.nid }}" class="btn-edit"
                               article_id="{{ article.nid }}">
                                <button type="button" class="btn btn-success ">编辑</button>
                            </a>

                            <!-- Button trigger modal -->
                            <button type="button" class="btn btn-danger " data-toggle="modal" data-target="#myModal">
                                删除
                            </button>
                            <!-- Modal -->
                            <div class="modal fade" id="myModal" tabindex="-1" role="dialog"
                                 aria-labelledby="myModalLabel">
                                <div class="modal-dialog" role="document">
                                    <div class="modal-content">
                                        <div class="modal-body">
                                            确认要将该篇文章删除么？

                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-default" data-dismiss="modal">取消
                                            </button>
                                            <button type="button" class="btn btn-primary btn-delete"
                                                    article_id="{{ article.nid }}" data-dismiss="modal">确认
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <a href="/{{ username }}/addarticle">
                                <button type="button" class="btn btn-info">新增</button>
                            </a>
                        </td>
                    </tr>
                {% endfor %}

                </tbody>
            </table>


        </div>

        <script src="/static/js/jquery.js"></script>

        <script>
            //删除文章
            $(".btn-delete").click(function () {
                $.ajax({
                    url: "/delete/",
                    type: "post",
                    data: {
                        "csrfmiddlewaretoken": $("[name='csrfmiddlewaretoken']").val(),
                        article_id: $(this).attr("article_id")
                    }, success: function (data) {
                        console.log(data)
                        location.href = "/{{ username }}/manager"
                    }
                })
            })

        </script>

{% endblock %}